package com.fw.ui.view.settings.usercontrol.bouser;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.service.bean.EntryBean;
import com.fw.service.bouser.bean.BOUserBean;
import com.fw.ui.bao.BOUserBAO;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.fw.ui.view.settings.usercontrol.bouser.event.DeleteBOUserCancelButtonClickListener;
import com.fw.ui.view.settings.usercontrol.bouser.event.DeleteBOUserDeleteButtonClickListener;
import com.fw.util.StringUtils;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinService;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "settings-user-control-bouser-delete", path = "/settings/user-control/bouser-delete")
public class DeleteBOUserView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String DELETE_BO_USER_BOUSER_ID_REQUEST_ATTRIBUTE = "DELETE_BO_USER_BOUSER_ID_REQUEST_ATTRIBUTE";

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private BOUserBAO boUserBAO;
    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;

    /**
     * BEAN
     */
    private BOUserBean boUserBean = new BOUserBean();

    /**
     * EVENT
     */
    @Autowired
    private DeleteBOUserCancelButtonClickListener boUserCancelButtonClickListener;
    @Autowired
    private DeleteBOUserDeleteButtonClickListener boUserDeleteButtonClickListener;

    /**
     * COMPONENT
     */
    private Panel deleteBOUserPanel = new Panel();

    private Label lblUsername = new Label("", ContentMode.HTML);
    private Label lblEmail = new Label("", ContentMode.HTML);
    private Label lblEmployeeId = new Label("", ContentMode.HTML);
    private Label lblEmployeeName = new Label("", ContentMode.HTML);
    private Label lblGroup = new Label("", ContentMode.HTML);
    private Label lblStatus = new Label("", ContentMode.HTML);
    private Label lblExpireDate = new Label("", ContentMode.HTML);
    private Label lblCreateDate = new Label("", ContentMode.HTML);
    private Label lblCreateBy = new Label("", ContentMode.HTML);
    private Label lblLastUpdateDate = new Label("", ContentMode.HTML);
    private Label lblLastUpdateBy = new Label("", ContentMode.HTML);

    private Label lblConfirmDeleteMessage = new Label("", ContentMode.HTML);

    private Button btnDelete = new Button();
    private Button btnCancel = new Button();

    private TextField txtUsername = new TextField();
    private TextField txtEmail = new TextField();
    private TextField txtEmployeeId = new TextField();
    private TextField txtEmployeeName = new TextField();
    private ComboBox cbbGroup = new ComboBox();
    private ComboBox cbbStatus = new ComboBox();
    private TextField txtExpireDate = new TextField();
    private TextField txtCreateDate = new TextField();
    private TextField txtCreateBy = new TextField();
    private TextField txtLastUpdateDate = new TextField();
    private TextField txtLastUpdateBy = new TextField();

    @Override
    public void updateStrings() {
	this.lblUsername.setValue(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.username"));
	this.lblEmail.setValue(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.email"));
	this.lblEmployeeId.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.employee.id"));
	this.lblEmployeeName.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.employee.name"));
	this.lblGroup.setValue(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.group"));
	this.lblStatus.setValue(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.status"));
	this.lblExpireDate.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.expiredate"));
	this.lblCreateDate.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.createdate"));
	this.lblCreateBy.setValue(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.createby"));
	this.lblLastUpdateDate.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.lastupdatedate"));
	this.lblLastUpdateBy.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.lastupdateby"));

	this.lblConfirmDeleteMessage.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.label.confirmdelete"));

	this.btnDelete.setCaption(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.button.delete"));
	this.btnCancel.setCaption(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.button.cancel"));

	this.deleteBOUserPanel.setCaption(this.messageBAO.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.title"));
    }

    @Override
    public void init() {
	this.setSizeFull();

	this.deleteBOUserPanel.setSizeFull();
	this.deleteBOUserPanel.setId("deleteBOUserPanel");

	this.lblUsername.setWidth("150px");
	this.lblEmail.setWidth("150px");
	this.lblEmployeeId.setWidth("150px");
	this.lblEmployeeName.setWidth("150px");
	this.lblGroup.setWidth("150px");
	this.lblStatus.setWidth("150px");
	this.lblExpireDate.setWidth("150px");
	this.lblCreateDate.setWidth("150px");
	this.lblCreateBy.setWidth("150px");
	this.lblLastUpdateDate.setWidth("150px");
	this.lblLastUpdateBy.setWidth("150px");

	this.cbbGroup.setWidth("200px");
	this.cbbStatus.setWidth("200px");
	this.txtUsername.setWidth("200px");
	this.txtEmail.setWidth("200px");
	this.txtEmployeeId.setWidth("200px");
	this.txtEmployeeName.setWidth("200px");
	this.txtExpireDate.setWidth("200px");
	this.txtCreateDate.setWidth("200px");
	this.txtCreateBy.setWidth("200px");
	this.txtLastUpdateDate.setWidth("200px");
	this.txtLastUpdateBy.setWidth("200px");

	this.lblConfirmDeleteMessage.addStyleName("label-red");

	this.btnDelete.setId("btnDelete" + UICommonUtils.EXCLUDE_ENABLE_COMPONENT_PREFIX);
	this.btnDelete.addStyleName("button-red");
	this.btnDelete.setDisableOnClick(true);
	this.btnDelete.setWidth("70px");

	this.btnCancel.setId("btnCancel" + UICommonUtils.EXCLUDE_ENABLE_COMPONENT_PREFIX);
	this.btnCancel.addStyleName("button-blue");
	this.btnCancel.setDisableOnClick(true);
	this.btnCancel.setWidth("70px");

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");

	this.deleteBOUserPanel.setContent(mainLayout);

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formPanel");
	formLayout.setSizeUndefined();
	formLayout.setWidth("100%");
	formLayout.setSpacing(true);

	HorizontalLayout row0 = new HorizontalLayout();
	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();
	HorizontalLayout row3 = new HorizontalLayout();
	HorizontalLayout row4 = new HorizontalLayout();
	HorizontalLayout row5 = new HorizontalLayout();
	HorizontalLayout row6 = new HorizontalLayout();
	HorizontalLayout row7 = new HorizontalLayout();
	HorizontalLayout row8 = new HorizontalLayout();
	HorizontalLayout row9 = new HorizontalLayout();
	HorizontalLayout row10 = new HorizontalLayout();
	HorizontalLayout row11 = new HorizontalLayout();
	HorizontalLayout row12 = new HorizontalLayout();
	HorizontalLayout row13 = new HorizontalLayout();
	HorizontalLayout row14 = new HorizontalLayout();

	row1.setSpacing(true);
	row2.setSpacing(true);
	row3.setSpacing(true);
	row4.setSpacing(true);
	row5.setSpacing(true);
	row6.setSpacing(true);
	row7.setSpacing(true);
	row8.setSpacing(true);
	row9.setSpacing(true);
	row10.setSpacing(true);
	row11.setSpacing(true);
	row12.setSpacing(true);
	row13.setSpacing(true);

	row1.setSizeUndefined();
	row2.setSizeUndefined();
	row3.setSizeUndefined();
	row4.setSizeUndefined();
	row5.setSizeUndefined();
	row6.setSizeUndefined();
	row7.setSizeUndefined();
	row8.setSizeUndefined();
	row9.setSizeUndefined();
	row10.setSizeUndefined();
	row11.setSizeUndefined();
	row12.setSizeUndefined();
	row13.setSizeUndefined();

	row1.setWidth("400px");
	row2.setWidth("400px");
	row3.setWidth("400px");
	row4.setWidth("400px");
	row5.setWidth("400px");
	row6.setWidth("400px");
	row7.setWidth("400px");
	row8.setWidth("400px");
	row9.setWidth("400px");
	row10.setWidth("400px");
	row11.setWidth("400px");

	row1.addComponent(lblUsername);
	row1.addComponent(txtUsername);
	row1.setComponentAlignment(lblUsername, Alignment.MIDDLE_RIGHT);
	row1.setComponentAlignment(txtUsername, Alignment.MIDDLE_LEFT);

	row2.addComponent(lblEmail);
	row2.addComponent(txtEmail);
	row2.setComponentAlignment(lblEmail, Alignment.MIDDLE_RIGHT);
	row2.setComponentAlignment(txtEmail, Alignment.MIDDLE_LEFT);

	row3.addComponent(lblEmployeeId);
	row3.addComponent(txtEmployeeId);
	row3.setComponentAlignment(lblEmployeeId, Alignment.MIDDLE_RIGHT);
	row3.setComponentAlignment(txtEmployeeId, Alignment.MIDDLE_LEFT);

	row4.addComponent(lblEmployeeName);
	row4.addComponent(txtEmployeeName);
	row4.setComponentAlignment(lblEmployeeName, Alignment.MIDDLE_RIGHT);
	row4.setComponentAlignment(txtEmployeeName, Alignment.MIDDLE_LEFT);

	row5.addComponent(lblGroup);
	row5.addComponent(cbbGroup);
	row5.setComponentAlignment(lblGroup, Alignment.MIDDLE_RIGHT);
	row5.setComponentAlignment(cbbGroup, Alignment.MIDDLE_LEFT);

	row6.addComponent(lblStatus);
	row6.addComponent(cbbStatus);
	row6.setComponentAlignment(lblStatus, Alignment.MIDDLE_RIGHT);
	row6.setComponentAlignment(cbbStatus, Alignment.MIDDLE_LEFT);

	row7.addComponent(lblExpireDate);
	row7.addComponent(txtExpireDate);
	row7.setComponentAlignment(lblExpireDate, Alignment.MIDDLE_RIGHT);
	row7.setComponentAlignment(txtExpireDate, Alignment.MIDDLE_LEFT);

	row8.addComponent(lblCreateDate);
	row8.addComponent(txtCreateDate);
	row8.setComponentAlignment(lblCreateDate, Alignment.MIDDLE_RIGHT);
	row8.setComponentAlignment(txtCreateDate, Alignment.MIDDLE_LEFT);

	row9.addComponent(lblCreateBy);
	row9.addComponent(txtCreateBy);
	row9.setComponentAlignment(lblCreateBy, Alignment.MIDDLE_RIGHT);
	row9.setComponentAlignment(txtCreateBy, Alignment.MIDDLE_LEFT);

	row10.addComponent(lblLastUpdateDate);
	row10.addComponent(txtLastUpdateDate);
	row10.setComponentAlignment(lblLastUpdateDate, Alignment.MIDDLE_RIGHT);
	row10.setComponentAlignment(txtLastUpdateDate, Alignment.MIDDLE_LEFT);

	row11.addComponent(lblLastUpdateBy);
	row11.addComponent(txtLastUpdateBy);
	row11.setComponentAlignment(lblLastUpdateBy, Alignment.MIDDLE_RIGHT);
	row11.setComponentAlignment(txtLastUpdateBy, Alignment.MIDDLE_LEFT);

	row12.addComponent(lblConfirmDeleteMessage);
	row12.setComponentAlignment(lblConfirmDeleteMessage, Alignment.MIDDLE_CENTER);

	row13.addComponent(btnDelete);
	row13.addComponent(btnCancel);

	formLayout.addComponent(row0);
	formLayout.addComponent(row1);
	formLayout.addComponent(row2);
	formLayout.addComponent(row3);
	formLayout.addComponent(row4);
	formLayout.addComponent(row5);
	formLayout.addComponent(row6);
	formLayout.addComponent(row7);
	formLayout.addComponent(row8);
	formLayout.addComponent(row9);
	formLayout.addComponent(row10);
	formLayout.addComponent(row11);
	formLayout.addComponent(row12);
	formLayout.addComponent(row13);
	formLayout.addComponent(row14);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row3, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row4, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row5, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row6, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row7, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row8, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row9, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row10, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row11, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row12, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row13, Alignment.MIDDLE_CENTER);

	mainLayout.addComponent(formLayout);
	mainLayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);

	this.addComponent(this.deleteBOUserPanel);
	this.setComponentAlignment(this.deleteBOUserPanel, Alignment.TOP_CENTER);
    }

    @Override
    public void initListener() {
	this.boUserCancelButtonClickListener.setBasedComponent(this);
	this.boUserDeleteButtonClickListener.setBasedComponent(this);

	this.boUserCancelButtonClickListener.setAutoEnabled(true);
	this.boUserDeleteButtonClickListener.setAutoEnabled(true);

	this.btnCancel.addClickListener(this.boUserCancelButtonClickListener);
	this.btnDelete.addClickListener(this.boUserDeleteButtonClickListener);
    }

    @Override
    public void initValidator() {

    }

    @Override
    public void initData() {
	try {
	    Long boUserId = (Long) VaadinService.getCurrentRequest().getAttribute(DELETE_BO_USER_BOUSER_ID_REQUEST_ATTRIBUTE);
	    if (boUserId == null) {
		String landing = propertyBAO.getPropertyValue("view.settings-user-control-bouser-search.path");
		this.getUI().getNavigator().navigateTo(landing);
	    } else {
		BOUserBean boUserBean = boUserBAO.getBOUser(boUserId);
		if (boUserBean == null) {
		    String msg = this.messageBAO.getMessage("message.error.common");
		    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
		    notification.setDelayMsec(1000);
		    notification.show(Page.getCurrent());
		} else {
		    this.setBoUserBean(boUserBean);

		    DateFormat dateFormat = new SimpleDateFormat(StringUtils.PATTERN_DATE_PERSISTENCE);
		    DateFormat dateTimeFormat = new SimpleDateFormat(StringUtils.PATTERN_DATETIME_PERSISTENCE);
		    DateFormat dateFormatUI = new SimpleDateFormat(StringUtils.PATTERN_DATE_UI);
		    DateFormat dateTimeFormatUI = new SimpleDateFormat(StringUtils.PATTERN_DATETIME_UI);

		    this.txtUsername.setValue(boUserBean.getUsername());
		    this.txtEmail.setValue(boUserBean.getEmail());
		    this.txtEmployeeId.setValue(boUserBean.getEmployeeID());
		    this.txtEmployeeName.setValue(boUserBean.getEmployeeName());

		    List<EntryBean<Long, String>> roles = masterDataBAO.selectAllBOUserRole();
		    if (roles != null) {
			for (EntryBean<Long, String> entry : roles) {
			    this.cbbGroup.addItem(entry.getValue());
			    this.cbbGroup.setItemCaption(entry.getValue(), entry.getValue());
			}
		    }

		    List<EntryBean<String, String>> statuses = masterDataBAO.selectBOUserStatusByCurrentStatus(boUserBean.getStatus());
		    if (statuses != null) {
			for (EntryBean<String, String> entry : statuses) {
			    this.cbbStatus.addItem(entry.getKey());
			    this.cbbStatus.setItemCaption(entry.getKey(), entry.getValue());
			}
		    }

		    this.cbbGroup.setValue(boUserBean.getRole());
		    this.cbbStatus.setValue(boUserBean.getStatus());

		    if (boUserBean.getExpiredDate() != null) {
			try {
			    Date date = dateFormat.parse(boUserBean.getExpiredDate());
			    String strUI = dateFormatUI.format(date);
			    this.txtExpireDate.setValue(strUI);
			} catch (Exception e) {
			}
		    }
		    if (boUserBean.getCreateDatetime() != null) {
			try {
			    Date date = dateTimeFormat.parse(boUserBean.getCreateDatetime());
			    String strUI = dateTimeFormatUI.format(date);
			    this.txtCreateDate.setValue(strUI);
			} catch (ParseException e) {
			}
		    }
		    if (boUserBean.getLastUpdateDatetime() != null) {
			try {
			    Date date = dateTimeFormat.parse(boUserBean.getLastUpdateDatetime());
			    String strUI = dateTimeFormatUI.format(date);
			    this.txtLastUpdateDate.setValue(strUI);
			} catch (ParseException e) {
			}
		    }

		    this.txtCreateBy.setValue(boUserBean.getCreateBy());
		    this.txtLastUpdateBy.setValue(boUserBean.getLastUpdateBy());

		    String activeStatus = propertyBAO
			    .getPropertyValue("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.exclude.status.item.active");
		    if (!activeStatus.equals(boUserBean.getStatus())) {
			this.btnDelete.setEnabled(false);
			this.lblConfirmDeleteMessage.setValue("");
			this.btnCancel.setCaption(this.messageBAO
				.getMessage("com.fw.ui.view.settings.usercontrol.bouser.DeleteBOUserView.button.close"));
		    }
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    String msg = this.messageBAO.getMessage("message.error.common");
	    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
	    notification.setDelayMsec(1000);
	    notification.show(Page.getCurrent());
	}

	uiCommonUtils.setEnableAllUIComponent(this, false);
    }

    public BOUserBean getBoUserBean() {
	return boUserBean;
    }

    public void setBoUserBean(BOUserBean boUserBean) {
	this.boUserBean = boUserBean;
    }

    public Button getBtnDelete() {
	return btnDelete;
    }

    public ComboBox getCbbStatus() {
	return cbbStatus;
    }

    public TextField getTxtLastUpdateDate() {
	return txtLastUpdateDate;
    }

    public TextField getTxtLastUpdateBy() {
	return txtLastUpdateBy;
    }

    public Label getLblConfirmDeleteMessage() {
	return lblConfirmDeleteMessage;
    }

    public Button getBtnCancel() {
	return btnCancel;
    }

}
